package com.lesama.stream;

/*
    Stream收集操作：
        把Stream流操作后的结果数据返回到集合
        R collect(Collector collector) 开始收集Stream流， 指定收集器

    Collectors 工具类提供了具体的收集方式：
        public static <T> Collector toList() 把元素收集到List集合中
        public static <T> Collector toSet() 把元素收集到Set集合中
        public static <T> Collector toMap(Function keyMapper, Function valueMapper) 把元素收集到Map集合中
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo4 {
    public static void main(String[] args) {
        extracted();
        System.out.println("--------------------------");
        List<String> list = new ArrayList<>();
        Collections.addAll(list, "爱丽希雅,25", "跳楼机,18", "昔涟,18");
        Map<String, Integer> map = list.stream().collect(Collectors.toMap(new Function<String, String>() {

            @Override
            public String apply(String s) {
                // s是流中的每个对象
                return s.split(",")[0];
            }
        }, new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return Integer.parseInt(s.split(",")[1]);
            }
        }));
        System.out.println(map);
    }

    private static void extracted() {
        List<String> list = Stream.of("爱丽希雅", "跳楼机", "昔涟").toList();
        System.out.println(list);
        Set<String> set = Stream.of("爱丽希雅", "跳楼机", "昔涟", "昔涟", "昔涟").collect(Collectors.toSet());
        System.out.println(set);
    }
}
